package com.yisu.auction.api.core.service.impl;

import com.yisu.auction.api.core.mapper.MemberMapper;
import com.yisu.auction.api.core.mapper.UserTokenMapper;
import com.yisu.auction.api.core.service.BaseService;
import com.yisu.auction.api.core.service.IMemberService;
import com.yisu.auction.api.domain.request.memberPackage.LoginRequest;
import com.yisu.auction.api.domain.request.memberPackage.MemberInfoRequest;
import com.yisu.auction.api.domain.request.memberPackage.MemberRequest;
import com.yisu.auction.api.domain.request.rechargePackage.RechargeRequest;
import com.yisu.auction.api.domain.vo.memberPackageVos.MemberBaseInfoVo;
import com.yisu.auction.ewf.common.utils.MD5Util;
import com.yisu.auction.ewf.entity.Member;
import com.yisu.auction.ewf.entity.UserToken;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * author: 赵杰
 * 业务描述：会员业务接口实现
 * 时间：2017/8/7
 */
@Service("IMemberService")
public class MemberServiceImpl extends BaseService implements IMemberService {
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private UserTokenMapper userTokenMapper;

    /**
     * 作者：赵杰
     * 业务：通过会员ID获取会员信息
     *
     * @param memberId 会员ID
     * @return 会员信息
     */
    @Override
    public Member getMemberByMemberId(long memberId) {
        return memberMapper.selectByPrimaryKey(memberId);
    }

    /**
     * 作者：赵杰
     * 业务：修改会员信息
     *
     * @param updateModel 修改的会员信息
     * @return 会员信息
     */
    @Override
    public boolean updateMemberModel(Member updateModel) {
        return memberMapper.updateByPrimaryKeySelective(updateModel) > 0;
    }

    /**
     * 作者：赵杰
     * 业务：通过手机号码及密码获取用户信息
     *
     * @param loginRequest 含会员手机号 密码
     * @return
     */
    @Override
    public Member getMemberByMobileAndPassword(LoginRequest loginRequest) {
        return memberMapper.getMemberByMobileAndPassword(loginRequest);
    }

    /**
     * 作者：赵杰
     * 业务：通过手机号码
     *
     * @param memberRequest 含会员手机号
     * @return
     */
    @Override
    public Member getMemberByMobile(MemberRequest memberRequest) {
        return memberMapper.getMemberByMobile(memberRequest);
    }

    /**
     * 作者：赵杰
     * 业务：通过用户token获取用户令牌信息
     *
     * @param token 令牌号
     * @return
     */
    @Override
    public UserToken getUserTokenByToken(String token) {
        UserToken userToken = null;
        try {
            userToken = userTokenMapper.getUserTokenByToken(token);
        } catch (Exception e) {
            logger.error("通过用户token获取用户令牌信息" + e.getMessage());
        }
        return userToken;
    }

    /**
     * 作者：赵杰
     * 业务：通过MemberId,获取用户令牌信息
     *
     * @param memberId 会员ID
     * @return
     */
    @Override
    public UserToken getUserTokenByMemberId(long memberId) {
        UserToken userToken = null;
        try {
            userToken = userTokenMapper.getUserTokenByMemberId(memberId);
            if (userToken == null) {
                String token = String.valueOf(java.util.UUID.randomUUID()).replace("-", "");
                userToken = new UserToken();
                userToken.setMemberId(memberId);
                userToken.setCreateDate(new Date());
                userToken.setToken(token);
                Date now = new Date();
                Calendar date = Calendar.getInstance();
                date.setTime(now);
                date.add(Calendar.YEAR, 20);
                userToken.setExpirationTime(date.getTime());
                userTokenMapper.insertSelective(userToken);
            }
        } catch (Exception e) {
            logger.error("通过MemberId,获取用户令牌信息异常" + e.getMessage());
        }
        return userToken;
    }

    /**
     * author: 赵杰
     * Business Description：通过第三方帐号及appSign获取会员基本信息
     *
     * @param memberRequest thirdID ，appSign
     * @return 会员基本信息
     */
    @Override
    public Member getMemberByThirdIdAndAppSign(MemberRequest memberRequest) {
        return memberMapper.getMemberByThirdIdAndAppSign(memberRequest);
    }

    /**
     * author: 赵杰
     * Business Description：通过第三方帐号及appSign 检测用户是否存在
     *
     * @param memberRequest thirdID ，appSign
     * @return 会员基本信息
     */
    @Override
    public boolean checkMemberByThirdIdAndAppSign(MemberRequest memberRequest) {
        return memberMapper.checkMemberByThirdIdAndAppSign(memberRequest) > 0;
    }

    /**
     * author: 赵杰
     * Business Description：添加会员基本信息
     *
     * @param member 会员基本信息
     * @return 成功，失败
     */
    @Override
    public MemberBaseInfoVo addMember(Member member) {
        MemberBaseInfoVo memberBaseInfoVo = new MemberBaseInfoVo();
        String token = String.valueOf(java.util.UUID.randomUUID()).replace("-", "");
        Long maxMemberId = Long.valueOf(memberMapper.getMaxMemberId()) == 0 ? (Long.valueOf(memberMapper.getMaxMemberId()) + 10000001) : (Long.valueOf(memberMapper.getMaxMemberId() + 1));
        member.setId(maxMemberId);
        if (memberMapper.insertSelective(member) > 0) {
            UserToken userToken = new UserToken();
            userToken.setMemberId(maxMemberId);
            userToken.setCreateDate(new Date());
            userToken.setToken(token);
            Date now = new Date();
            Calendar date = Calendar.getInstance();
            date.setTime(now);
            date.add(Calendar.YEAR, 20);
            userToken.setExpirationTime(date.getTime());
            if (userTokenMapper.insertSelective(userToken) > 0) {
                BigDecimal bg = new BigDecimal("0");
                memberBaseInfoVo.setAddress(member.getAddress());
                memberBaseInfoVo.setIco(member.getIco());
                memberBaseInfoVo.setId(member.getId());
                memberBaseInfoVo.setToken(token);
                memberBaseInfoVo.setWallet(bg);
                memberBaseInfoVo.setPresentWallet(bg);
                memberBaseInfoVo.setNickName(member.getNickname());
                memberBaseInfoVo.setMobile(member.getMobile());
                return memberBaseInfoVo;
            }
        }
        return null;
    }


    @Override
    public MemberBaseInfoVo getMemberBaseInfo(MemberRequest memberRequest) {
        return memberMapper.getMemberBaseInfo(memberRequest);
    }

    /**
     * author:黄萧林
     * Business Description：更新用户信息
     *
     * @param memberInfoRequest
     * @return
     */
    @Override
    public boolean updateMemberIco(MemberInfoRequest memberInfoRequest) {
        Member member = new Member();
        member.setId(Long.valueOf(memberInfoRequest.getMemberId()));
        member.setIco(memberInfoRequest.getIco());
        return memberMapper.updateByPrimaryKeySelective(member) > 0;
    }

    /**
     * author:黄萧林
     * Business Description:更新用户名字
     *
     * @param memberInfoRequest
     * @return
     */
    @Override
    public boolean updateMemberName(MemberInfoRequest memberInfoRequest) {
        Member member = new Member();
        member.setId(Long.valueOf(memberInfoRequest.getMemberId()));
        member.setNickname(memberInfoRequest.getMemberName());
        return memberMapper.updateByPrimaryKeySelective(member) > 0;
    }

    /**
     * author:黄萧林
     * Business Description:更新用户密码
     *
     * @param memberInfoRequest
     * @return
     */
    @Override
    public boolean updateMemberPasswordForNoLogin(MemberInfoRequest memberInfoRequest) {
        return memberMapper.updateMemberPasswordForNoLogin(memberInfoRequest) > 0;
    }

    /**
     * author:黄萧林
     * Business Description:更新用户手机号
     *
     * @param memberInfoRequest
     * @return
     */
    @Override
    public boolean updateMemberPhoneNumber(MemberInfoRequest memberInfoRequest) {
        Member member = new Member();
        member.setId(Long.valueOf(memberInfoRequest.getMemberId()));
        member.setMobile(memberInfoRequest.getPhoneNumber());
        return memberMapper.updateByPrimaryKeySelective(member) > 0;
    }

    /**
     * author:黄萧林
     * Business Description:判断用户是否为新手
     *
     * @param rechargeRequest
     * @return
     */
    @Override
    public int returnIsNewUser(RechargeRequest rechargeRequest) {
        Member member = memberMapper.selectByPrimaryKey(Long.valueOf(rechargeRequest.getMemberId()));
        if (member.getConsumptionAmount().doubleValue() > 0) {
            return 0;
        } else {
            return 1;
        }
    }
}
